//
//  MBDAboutViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/3/28.
//

import Foundation
import UIKit
import YYText
import StoreKit

class MBDAboutViewController : UIViewController {
    var tableView: UITableView!
    var aboutItems : NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()
        aboutItems = ["去评分","版本更新"]

        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        let headerView = UIView()
        let frame = CGRectMake(0, 0, UIScreen.main.bounds.width, 200)
        headerView.frame = frame
        
        let logoImageView = UIImageView()
        logoImageView.image = UIImage(named: "AppIcon")
        headerView.addSubview(logoImageView)
        logoImageView.snp.makeConstraints { make in
            make.centerX.equalTo(headerView)
            make.centerY.equalTo(headerView).offset(-30)
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        tableView.tableHeaderView = headerView
        
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
                    self.view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
                    self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
                    self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
                ])
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 100

        tableView.register(MBDAboutCommonCell.self, forCellReuseIdentifier: "MBDAboutCommonCell")
        
    }

}

extension MBDAboutViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.aboutItems.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MBDAboutCommonCell", for: indexPath)
        (cell as! MBDAboutCommonCell ).title = self.aboutItems[indexPath.row] as! String
        if(0 == indexPath.row) {
            (cell as! MBDAboutCommonCell ).location = .top
        }
        if(self.aboutItems.count-1 == indexPath.row) {
            (cell as! MBDAboutCommonCell ).location = .bottom
        }
        return cell
    }
}


extension MBDAboutViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if(0 == indexPath.row) {
            SKStoreReviewController.requestReview()

        } else if (1 == indexPath.row){
            self.alertsTwo()
        }
        
    }
    
    //MARK: - 双按钮提示框
    func alertsTwo(){
        //https://apps.apple.com/cn/app/%E7%BE%A4%E7%AE%A1%E5%AE%B6PRO/id6449223647
        let updateUrl:URL = URL.init(string: "https://apps.apple.com/cn/app/%E7%BE%A4%E7%AE%A1%E5%AE%B6PRO/id6449223647")!
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(updateUrl, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(updateUrl)
        }
        self.dismiss(animated: true, completion: nil)
    }
}



class MBDAboutCommonCell: MBDCommonCell {

    var titleLabel:YYLabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    func initialize() {
        titleLabel = YYLabel()
        self.commonBackgroundView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(commonBackgroundView)
            make.top.equalTo(14)
            make.left.equalTo(20)
        }

    }
    
    var title : String? {
        didSet {
            
            let usernameAttributeString = NSMutableAttributedString(string: self.title ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.yy_color = UIColor(colorHex: 0x333333)
            
            titleLabel.attributedText = usernameAttributeString
        }
        
        willSet {
        }
    }
    
}
